﻿package frEngine
{
    import __AS3__.vec.*;
    import baseEngine.system.*;
    import flash.utils.*;

    public class TimeControler extends Object
    {
        private var _offFrame:int = 0;
        public var curFrame:int = 0;
        public var _totalframe:int = 1;
        public var duringFrame:Number = 1.79769e+308;
        public var id:Object;
        public var autoPlay:Boolean = true;
        private var _childrent:Vector.<TimeControler>;
        private var _parent:TimeControler;
        public var isActive:Boolean = true;
        private var _key:int = -1;
        public static const instanceList:Dictionary = new Dictionary(false);
        public static const activeList:Dictionary = new Dictionary(false);
        public static const defaultTimeControler:TimeControler = createTimerInstance(-1);
        public static var stageFrame:uint = 0;
        public static var stageTime:uint = 0;
        public static var minFpsTime:Number = 10;
        private static var _timeId:int = 0;

        public function TimeControler(param1:Object)
        {
            this.id = param1;
            return;
        }// end function

        public function set totalframe(param1:uint) : void
        {
            var _loc_2:TimeControler = null;
            this._totalframe = param1;
            if (this._childrent)
            {
                for each (_loc_2 in this._childrent)
                {
                    
                    _loc_2.totalframe = this._totalframe - _loc_2._offFrame;
                    _loc_2.curFrame = _loc_2.totalframe % _loc_2.duringFrame;
                }
            }
            return;
        }// end function

        public function get parent() : TimeControler
        {
            return this._parent;
        }// end function

        public function get totalframe() : uint
        {
            return this._totalframe;
        }// end function

        public function gotoFrame(param1:uint, param2:uint) : void
        {
            if (this.id == -1)
            {
                return;
            }
            this.curFrame = param1;
            this.totalframe = param2;
            if (this._parent)
            {
                this._offFrame = this._parent.totalframe - this.totalframe;
            }
            else
            {
                this._offFrame = TimeControler.stageFrame - this.totalframe;
            }
            return;
        }// end function

        public function active(param1:int = -1) : void
        {
            if (this._key != param1)
            {
                return;
            }
            this.isActive = true;
            activeList[this.id] = this;
            return;
        }// end function

        public function unActive(param1:int = -1) : void
        {
            if (this.id == -1 || this._key != param1)
            {
                return;
            }
            this.isActive = false;
            delete activeList[this.id];
            return;
        }// end function

        public static function upDateTime(param1:uint) : void
        {
            var _loc_3:TimeControler = null;
            stageTime = param1;
            var _loc_2:* = uint(stageTime / 1000 * Global3D.animateSpeedFrame);
            TimeControler.stageFrame = _loc_2;
            for each (_loc_3 in activeList)
            {
                
                if (_loc_3.autoPlay)
                {
                    _loc_3.totalframe = _loc_2 - _loc_3._offFrame;
                    _loc_3.curFrame = _loc_3.totalframe % _loc_3.duringFrame;
                }
            }
            return;
        }// end function

        public static function createTimerInstance(param1:Object = null, param2:TimeControler = null, param3:int = -1) : TimeControler
        {
            var _loc_4:TimeControler = null;
            var _loc_5:int = 0;
            if (param1 == null)
            {
                param1 = _timeId;
                var _loc_7:* = _timeId + 1;
                _timeId = _loc_7;
            }
            if (param2)
            {
                _loc_4 = new TimeControler(param1);
                _loc_4._key = param3;
                if (param2._childrent == null)
                {
                    param2._childrent = new Vector.<TimeControler>;
                }
                _loc_5 = param2._childrent.indexOf(_loc_4);
                if (_loc_5 == -1)
                {
                    param2._childrent.push(_loc_4);
                }
                _loc_4._parent = param2;
            }
            else
            {
                _loc_4 = instanceList[param1];
                if (!_loc_4)
                {
                    var _loc_6:* = new TimeControler(param1);
                    _loc_4 = new TimeControler(param1);
                    instanceList[param1] = _loc_6;
                    _loc_4._key = param3;
                }
            }
            _loc_4.active(param3);
            return _loc_4;
        }// end function

        public static function disposeTimer(param1:TimeControler, param2:int = -1) : void
        {
            var _loc_4:TimeControler = null;
            if (param1.id == -1 || param1._key != param2)
            {
                return;
            }
            var _loc_3:* = param1._childrent;
            if (_loc_3)
            {
                for each (_loc_4 in _loc_3)
                {
                    
                    _loc_4._parent = null;
                }
            }
            param1._childrent = null;
            param1.unActive();
            delete TimeControler.instanceList[param1.id];
            return;
        }// end function

    }
}
